//
//  OrderListViewController.m
//  PigMarket
//
//  Created by 张敬 on 2019/8/13.
//  Copyright © 2019年 pigmarket.oc. All rights reserved.
//

#import "OrderListViewController.h"
#import "BaseNavView.h"
#import "OrderTableViewController.h"
#import "MeatCartViewController.h"
#import "RecentlyChatListViewController.h"
#import "OrderHotWordViewController.h"

#pragma mark - Test
#import "OrderSearchResultViewController.h"
#import "OrderEvaluationViewController.h"
#import "OrderAgainCommentViewController.h"
#import "OrderCommentSuccessViewController.h"






@interface OrderListViewController ()

@property (nonatomic, strong) NSArray *titles;
@property (nonatomic, strong) NSArray *controls;
@property (nonatomic, strong) BaseNavView *header;
@property (nonatomic, strong) NSArray *icons;
@property (nonatomic, strong) UIImageView *showIcon;


@end

@implementation OrderListViewController

#pragma mark - view life cycle
- (void)viewDidLoad {
    [super viewDidLoad];
    [self setUpView];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = YES;
}


#pragma mark - init method

- (void)setUpView {
    [self setupNavBar];
    [self setupChildViewControllers];
    [self setupChildSetting];
    
    UIWindow *window = [[UIApplication sharedApplication] keyWindow];
    [window addSubview:self.showIcon];

}

- (void)setupNavBar{
    [self.view addSubview:self.header];
}

- (void)setupChildViewControllers{
    
    @weakify(self);
    [self.controls enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        @strongify(self);
        
        OrderTableViewController *vc = (OrderTableViewController *)obj;
        vc.title = self.titles[idx];
        vc.jumpBlock = ^(NSInteger params) {
            self.selectIndex = params;
        };
        [self addChildViewController:vc];
    }];
}

- (void)setupChildSetting
{
    [self setUpContentViewFrame:^(UIView *contentView) {
        CGFloat contentX = 0;
        CGFloat contentH = KScreenHeight - TJ_NavBarHeight ;
        contentView.frame = CGRectMake(contentX, TJ_NavBarHeight, KScreenWidth, contentH);
    }];
    
    [self setUpTitleEffect:^(UIColor *__autoreleasing *titleScrollViewColor, UIColor *__autoreleasing *norColor, UIColor *__autoreleasing *selColor, UIFont *__autoreleasing *titleFont, CGFloat *titleHeight,CGFloat *titleWidth) {
        
        // 设置标题字体
        *titleFont = mMediumFont(14);
        *norColor = UIColorFromRGB(0x4F4F4F);
        *selColor = mMainColor;
        *titleHeight = 42;
        *titleScrollViewColor = KWhiteColor;
    }];
    
    // 推荐方式（设置下标）
    [self setUpUnderLineEffect:^(BOOL *isUnderLineDelayScroll, CGFloat *underLineH, UIColor *__autoreleasing *underLineColor,BOOL *isUnderLineEqualTitleWidth) {
        // 标题填充模式
        *underLineColor = mMainColor;
        *isUnderLineEqualTitleWidth = YES;
        *underLineH = 2;
    }];
    
}

- (void)addList{
    
    __block CGFloat gap = 40.f;
    @weakify(self);
    [self.icons enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        
        @strongify(self);
        
        UIImageView *icon = [UIImageView makeImageByImageName:obj cornerRadius:0];
        icon.size = CGSizeMake(40, 40 );
        icon.mj_y = TJ_StatusBarHeight ;
        icon.mj_x = KScreenWidth - 2*gap +gap*idx -10;
        icon.isNeedAnimation = YES;
        [self.header addSubview:icon];
        
        icon.tapBlock = ^{
            
            if (idx == 0) {
                
                if (self.showIcon.alpha == 1) {
                    [self hideShopIcon];
                }
                
                //点击搜索按钮
//                OrderHotWordViewController *vc = [[OrderHotWordViewController alloc]init];
//                vc.hotWords = @[];
//                UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
//                [self presentViewController:nav animated:YES completion:nil];
                
                OrderCommentSuccessViewController *vc = [[OrderCommentSuccessViewController alloc] init];
                [self.navigationController pushViewController:vc animated:YES];
                

            }else{
                
                if (self.showIcon.alpha == 0 ) {
                    
                    [UIView animateWithDuration:AnimationTime animations:^{
                        self.showIcon.alpha = 1;
                    }];
                    
                }else{
                    [self hideShopIcon];
                }

            }
        };
    }];
}

#pragma mark - setter methods

- (void)setCurrentIndex:(NSInteger)currentIndex{
    
    self.selectIndex = currentIndex;
}

#pragma mark - getter methods

- (UIImageView *)showIcon{
    
    if (!_showIcon) {
        _showIcon = [UIImageView makeImageByImageName:@"meat_shop_box_bg" cornerRadius:0];
        _showIcon.backgroundColor = KClearColor;
        _showIcon.size = CGSizeMake(144, 154);
        _showIcon.mj_x = KScreenWidth - _showIcon.width - 15;
        _showIcon.mj_y = TJ_StatusBarHeight + 40;
        _showIcon.alpha = 0;
        _showIcon.userInteractionEnabled = YES;
        
        NSArray *icons = @[@"chat_middle", @"icon_home", @"meat_buy_cart"];
        NSArray *titles = @[@"消息", @"首页", @"购物车"];
        
        @weakify(self);
        [icons enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            
            @strongify(self);
            UIImageView *icon = [UIImageView makeImageByImageName:obj cornerRadius:0];
            icon.userInteractionEnabled = YES;
            icon.size = CGSizeMake(22, 22);
            icon.mj_y = 18 + (28 + icon.height)*idx ;
            icon.mj_x = 14;
            [self.showIcon addSubview:icon];
            
            
            UILabel *label = [UILabel makeLabelWithText:titles[idx] textColor:UIColorFromRGB(0x4A4A4A) font:mFont(14) textAliment:0];
            label.size = CGSizeMake(144 - icon.right - 5, 22);
            label.mj_x = icon.right + 5;
            label.mj_y = icon.mj_y;
            [self.showIcon addSubview:label];
            
            UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
            btn.frame = CGRectMake(0, 50*idx, 144, 50);
            btn.tag = 200+idx;
            [self.showIcon addSubview:btn];
            
            [btn addTarget:self action:@selector(jumpRelatedController:) forControlEvents:UIControlEventTouchUpInside];
            
            if (idx >0) {
                CAShapeLayer *line = [CAShapeLayer layer];
                line.fillColor = UIColorFromRGB(0xEAEAEA).CGColor;
                UIBezierPath *path = [UIBezierPath bezierPathWithRect:CGRectMake(0, 50*idx, self.showIcon.width, 1)];
                line.path = path.CGPath;
                [self.showIcon.layer addSublayer:line];
            }
        }];
        
    }
    return _showIcon;
}


- (void)jumpRelatedController:(UIButton *)btn{

    switch (btn.tag) {
        case 200:
            {
                [self chat];
            }
            break;
            
        case 201:
            {
            
            }
            break;
            
        default:
            {
                MeatCartViewController *vc = [[MeatCartViewController alloc] init];
                [self.navigationController pushViewController:vc animated:YES];
            }
            break;
    }
    
    self.showIcon.alpha = 0;
}

//我想聊聊
- (void)chat {
    
    //校验聊天是否登陆成功，否则重新登录
    if ([UserInfoDataProvider sharedInstance].userInfo.isIMLogin) {
        
        @weakify(self);
        [self pushToViewController:^{
            @strongify(self);
            [self.navigationController pushViewController:[[RecentlyChatListViewController alloc] init] animated:YES];
        }];
        
    } else {
        //如果没登录过
        NSString *userSig = [UserInfoDataProvider sharedInstance].userInfo.userSig;
        NSString *userName = [UserInfoDataProvider sharedInstance].userInfo.memberNum;
        [[ChatManager sharedInstance] loginIMWithUserName:userName userSig:userSig succ:^{
            [self toastWithContent:@"聊天登录成功"];
            UserInfo *userInfo = [UserInfoDataProvider sharedInstance].userInfo;
            userInfo.isIMLogin = YES;
            [UserInfoDataProvider sharedInstance].userInfo = userInfo;
        } fail:^(int code, NSString * _Nonnull msg) {
            [self toastWithContent:msg];
        }];
        [self.navigationController pushViewController:[[RecentlyChatListViewController alloc] init] animated:YES];
    }
}


- (void)hideShopIcon{
    
    @weakify(self);
    [UIView animateWithDuration:AnimationTime animations:^{
        @strongify(self);
        self.showIcon.alpha = 0;
    }];
}

- (NSArray *)titles{
    if (!_titles) {
        _titles = @[@"全部",@"待付款",@"待收货",@"待评价",@"已完成"];
    }
    return _titles;
}

- (NSArray *)icons{
    
    if (!_icons) {
        _icons = @[@"meat_order_search", @"meat_order_more"];
    }
    return _icons;
}


- (NSArray *)controls
{
    if (!_controls) {

        OrderTableViewController *oneVC = [[OrderTableViewController alloc] init];
        OrderTableViewController *twoVC = [[OrderTableViewController alloc]init];
        OrderTableViewController *threeVC = [[OrderTableViewController alloc]init];
        OrderTableViewController *fourVC = [[OrderTableViewController alloc]init];
        OrderTableViewController *fiveVC = [[OrderTableViewController alloc]init];

        //0全部;1待付款2待发货3待收货5已完成4待评价
        oneVC.type = @0;
        twoVC.type = @1;
        threeVC.type = @2;
        fourVC.type = @4;
        fiveVC.type = @5;
        
        _controls = @[oneVC,twoVC,threeVC,fourVC,fiveVC];
    }
    return _controls;
}


- (BaseNavView *)header{
    if (!_header) {
        _header = [[BaseNavView alloc] initWithTitle:@"我的订单"];
        _header.width = KScreenWidth;
        _header.height = TJ_NavBarHeight;
        _header.line.hidden = YES;
        
        [self addList];
    }
    return _header;
}


@end
